{% extends "base.html" %}

{% block title %}端口扫描{% endblock %}

{% block active2 %}class="active-menu"{% endblock %}

{% block css %}

{% endblock %}

{% block body %}
                    <!-- 表格 -->
                    <div class="panel panel-default">
                        <div class="panel-heading">
                             端口扫描    <button type="button" class="btn btn-primary" id="addTargetBtn">添加目标</button>
                        </div>
                    <!-- 结果 -->
                        <div class="panel-body">
                        <!--检索模块 -->
                        <div>
                        <form method="GET" class="form-inline mb-3" id="select_filter" action="{{ url_for('portscan.select_port') }}">
                            <div class="form-group">
                                <label for="filter">搜索字段：</label>
                                <select class="form-control" name="filter" id="filter">
                                    <option value="target_ip" name="target_ip" {% if filter == 'target_ip' %}selected{% endif %}>IP地址</option>
                                    <option value="target_port" name="target_port" {% if filter == 'target_port' %}selected{% endif %}>端口号</option>
                                </select>
                            </div>
                            <div class="form-group">
                                <input type="text" class="form-control" name="q" placeholder="搜索关键字" value="{{ q }}">
                            </div>
                                <button type="submit" class="btn btn-primary" id="selectBtn">搜索</button>
                        </form>
                        </div>



                            <!-- 展示模块 -->
                            <div class="table-responsive">
                                <table class="table table-striped table-bordered table-hover" id="dataTables-example">
                                    <thead>
                                        <tr>
                                            <th> # </th>
                                            <th>IP地址</th>
                                            <th>端口</th>
                                            <th>状态</th>
                                            <th>服务</th>
                                            <th>版本</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                    {% for i in data %}
                                        <tr>
                                            <td>{{ i.id }}</td>
                                            <td>{{ i.target_ip }}</td>
                                            <td>{{ i.target_port }}</td>
                                            <td>{{ i.status}}</td>
                                            <td>{{ i.service }}</td>
                                            <td>{{ i.version }}</td>
                                        </tr>
                                    {% endfor %}
                                    <div class="pagination-container">
                                        <ul class="pagination"></ul>
                                    </div>
                                    </tbody>
                                </table>
                            </div>
                        <!--  -->
                        <!-- 分页模块 -->
                        <div>
  <nav class="mt-4">
    <ul class="pagination justify-content-center">
      {% if page > 1 %}
        <li class="page-item"><a class="page-link" href="{{ url_for('portscan.select_port', page=page - 1, filter=filter, q=q) }}">上一页</a></li>
      {% else %}
        <li class="page-item disabled"><span class="page-link">上一页</span></li>
      {% endif %}

      {% if total_pages <= 7 %}
        {% for p in range(1, total_pages + 1) %}
          {% if p == page %}
            <li class="page-item active"><a class="page-link" href="#">{{ p }}</a></li>
          {% else %}
            <li class="page-item"><a class="page-link" href="{{ url_for('portscan.select_port', page=p, filter=filter, q=q) }}">{{ p }}</a></li>
          {% endif %}
        {% endfor %}
      {% else %}
        {% if page <= 4 %}
          {% for p in range(1, 6) %}
            {% if p == page %}
              <li class="page-item active"><a class="page-link" href="#">{{ p }}</a></li>
            {% else %}
              <li class="page-item"><a class="page-link" href="{{ url_for('portscan.select_port', page=p, filter=filter, q=q) }}">{{ p }}</a></li>
            {% endif %}
          {% endfor %}
          <li class="page-item disabled"><span class="page-link">...</span></li>
          <li class="page-item"><a class="page-link" href="{{ url_for('portscan.select_port', page=total_pages, filter=filter, q=q) }}">{{ total_pages }}</a></li>
        {% elif page >= total_pages - 3 %}
          <li class="page-item"><a class="page-link" href="{{ url_for('portscan.select_port', page=1, filter=filter, q=q) }}">1</a></li>
          <li class="page-item disabled"><span class="page-link">...</span></li>
          {% for p in range(total_pages - 4, total_pages + 1) %}
            {% if p == page %}
              <li class="page-item active"><a class="page-link" href="#">{{ p }}</a></li>
            {% else %}
              <li class="page-item"><a class="page-link" href="{{ url_for('portscan.select_port', page=p, filter=filter, q=q) }}">{{ p }}</a></li>
            {% endif %}
          {% endfor %}
        {% else %}
          <li class="page-item"><a class="page-link" href="{{ url_for('portscan.select_port', page=1, filter=filter, q=q) }}">1</a></li>
          <li class="page-item disabled"><span class="page-link">...</span></li>
          {% for p in range(page - 2, page + 3) %}
            {% if p == page %}
              <li class="page-item active"><a class="page-link" href="#">{{ p }}</a></li>
            {% else %}
              <li class="page-item"><a class="page-link" href="{{ url_for('portscan.select_port', page=p, filter=filter, q=q) }}">{{ p }}</a></li>
            {% endif %}
          {% endfor %}
          <li class="page-item disabled"><span class="page-link">...</span></li>
          <li class="page-item"><a class="page-link" href="{{ url_for('portscan.select_port', page=total_pages, filter=filter, q=q) }}">{{ total_pages }}</a></li>
        {% endif %}
      {% endif %}

      {% if page < total_pages %}
        <li class="page-item"><a class="page-link" href="{{ url_for('portscan.select_port', page=page + 1, filter=filter, q=q) }}">下一页</a></li>
      {% else %}
        <li class="page-item disabled"><span class="page-link">下一页</span></li>
      {% endif %}
    </ul>
  </nav>
</div>

                        <!--添加目标页面-->
        <div class="modal fade" id="addTargetModal" tabindex="-1" role="dialog" aria-labelledby="addTargetModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="addTargetModalLabel">添加目标</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <form action="{{ url_for('portscan.port_scan') }}" method="post" id="submitTables">
                    <div class="form-group">
                        <label for="targetIpInput">IP地址：</label>
                        <input type="text" class="form-control" id="targetIpInput" name="target_ips" placeholder="如 127.0.0.1，192.168.1.1">
                    </div>
                    <div class="form-group">
                        <label for="targetPortInput">端口号：</label>
                        <input type="text" class="form-control" id="targetPortInput" name="target_ports" placeholder="如 21,22,80 (默认扫描常见端口号)">
                    </div>
                    <button type="submit" class="btn btn-primary" id="submitBtn">提交</button>
                </form>
            </div>
        </div>
    </div>
</div>
<!--  -->
                                                {% for msg in get_flashed_messages() %}
                                                           <div> {{ msg }}</div>
                                                                    {% endfor %}
                        </div>
                    </div>
    <!--End Advanced Tables -->
{% endblock %}

{% block js %}
<script>
    // 获取添加目标按钮的元素
    var addTargetBtn = document.getElementById("addTargetBtn");

    // 添加点击事件处理程序
    addTargetBtn.onclick = function() {
        // 获取模态框的元素
        var modal = document.getElementById("addTargetModal");
        // 显示模态框
        $(modal).modal("show");
    };

    // 获取表单提交按钮的元素
    var submitBtn = document.getElementById("submitBtn");

    // 添加点击事件处理程序
    submitBtn.onclick = function() {
        // 获取模态框的元素
        var modal = document.getElementById("addTargetModal");
        // 关闭模态框
        $(modal).modal("hide");
    };


    // 添加目标表单验证
        $("#submitTables").submit(function(event) {
            var targetIp = $("#targetIpInput").val();
            var targetPort = $("#targetPortInput").val();
            var ipRegex = /\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/g;
            var portRegex = /^\d+(,\d+)*$/;

            if (!ipRegex.test(targetIp)) {
                alert("请输入正确的IP地址！");
                event.preventDefault();
            }

            if (!portRegex.test(targetPort) && targetPort) {
                alert("请输入正确的端口号格式！");
                event.preventDefault();
            }
        });
</script>
{% endblock %}